﻿Public Class mnuPrincipal


    Public Sub btnUsuarios_Click(sender As Object, e As EventArgs) Handles btnUsuarios.Click

        'Si el usuario no es Admin entonces no permito ingresar
        If varIdUsuarioSistema <> 1 Then
            MsgBox("No tiene permisos", vbOKOnly + vbExclamation, "Atención")
            Exit Sub
        End If

        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor

        'Si hay imagen de fondo la remuevo
        If SplitContainer1.Panel2.BackgroundImage IsNot Nothing Then
            SplitContainer1.Panel2.BackgroundImage = Nothing
        End If

        'Remuevo user control anterior
        For Each uc As UserControl In SplitContainer1.Panel2.Controls
            SplitContainer1.Panel2.Controls.Remove(uc)
        Next

        'Cargo el user control
        varUcUsuarios = New ucUsuarios
        SplitContainer1.Panel2.Controls.Add(varUcUsuarios)
        varUcUsuarios.BringToFront()
        varUcUsuarios.Dock = DockStyle.Fill

        'Cargo la grilla
        sqlConsulta = "SELECT u.Id, CONCAT(Nombre, ' ', Apellido) As ""Nombre y apellido"", Username As Usuario, Email, Telefono, r.Name As Rango"
        sqlConsulta = sqlConsulta & " FROM users u LEFT JOIN roles r ON u.rango = r.Id"
        sqlConsulta = sqlConsulta & " Where u.Activo=1"
        If varNombreUsuarioSistema <> "ls" Then sqlConsulta = sqlConsulta & " AND u.Id<>1"

        cn.LlenarGrilla(varUcUsuarios.dgvUsuarios, sqlConsulta)

        Me.Cursor = System.Windows.Forms.Cursors.Default

    End Sub

    Private Sub btnTratadoras_Click(sender As Object, e As EventArgs) Handles btnTratadoras.Click

        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor

        'Si hay imagen de fondo la remuevo
        If SplitContainer1.Panel2.BackgroundImage IsNot Nothing Then
            SplitContainer1.Panel2.BackgroundImage = Nothing
        End If

        'Remuevo user control anterior
        For Each uc As UserControl In SplitContainer1.Panel2.Controls
            SplitContainer1.Panel2.Controls.Remove(uc)
        Next

        'Cargo el user control
        varUcTratadoras = New ucTratadoras
        SplitContainer1.Panel2.Controls.Add(varUcTratadoras)
        varUcTratadoras.BringToFront()
        varUcTratadoras.Dock = DockStyle.Fill

        'Cargo la grilla
        sqlConsulta = "SELECT Id, modelo as Nombre, Model As Modelo, Serie FROM tratadoras Where Activo=1"
        'sqlConsulta = sqlConsulta & " FROM users u LEFT JOIN roles r ON u.rango = r.Id"
        cn.LlenarGrilla(varUcTratadoras.dgvTratadoras, sqlConsulta)

        'Oculto campos innecesarios
        varUcTratadoras.dgvTratadoras.Columns("Id").Visible = False
        varUcTratadoras.dgvTratadoras.Columns("Modelo").Visible = False

        'Si el usuario no es admin deshabilito botones de ABM
        If varIdUsuarioSistema <> 1 Then
            Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()

            Dim backGroundImage As New Bitmap(asm.GetManifestResourceStream("Tratadora.imgTratadora_alta_off.jpg"))
            varUcTratadoras.btnAlta.BackgroundImage = backGroundImage
            varUcTratadoras.btnAlta.Enabled = False

            Dim backGroundImage2 As New Bitmap(asm.GetManifestResourceStream("Tratadora.imgTratadora_eliminar_off.jpg"))
            varUcTratadoras.btnEliminar.BackgroundImage = backGroundImage2
            varUcTratadoras.btnEliminar.Enabled = False

            Dim backGroundImage3 As New Bitmap(asm.GetManifestResourceStream("Tratadora.imgTratadora_modificacion_off.jpg"))
            varUcTratadoras.btnModificacion.BackgroundImage = backGroundImage3
            varUcTratadoras.btnModificacion.Enabled = False

        End If

        Me.Cursor = System.Windows.Forms.Cursors.Default


    End Sub

    Private Sub mnuPrincipal_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Me.Text = "v" & My.Application.Info.Version.ToString & " " & My.Application.Info.Description

        'Si el usuario no es Admin entonces deshabilito los botones
        If varIdUsuarioSistema <> 1 Then
            DeshabilitarBotones()
        End If

        'Imagen de fondo
        'SplitContainer1.Panel2.BackgroundImage = System.Drawing.Image.FromFile(Application.StartupPath() & "\inicio.jpg")
        btnInicio_Click(Nothing, Nothing)

    End Sub

    Private Sub btnInicio_Click(sender As Object, e As EventArgs) Handles btnInicio.Click

        'Remuevo user control anterior
        For Each uc As UserControl In SplitContainer1.Panel2.Controls
            SplitContainer1.Panel2.Controls.Remove(uc)
        Next

        'Imagen de fondo
        Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
        Dim backGroundImage As New Bitmap(asm.GetManifestResourceStream("Tratadora.imgInicio.jpg"))
        SplitContainer1.Panel2.BackgroundImage = backGroundImage

    End Sub

    Private Sub DeshabilitarBotones()

        Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
        Dim backGroundImage As New Bitmap(asm.GetManifestResourceStream("Tratadora.imgUser_off.jpg"))
        btnUsuarios.BackgroundImage = backGroundImage
        btnUsuarios.Enabled = False

    End Sub
End Class
